<!--
 作者：ex_baiyc
 日期：2016-10-11
 功能：知己彼--产品整体对标
 参数：
 事件：
 v-link='{path: "/peer/brandDetailedEvaluation?key=brandDetailedEvaluation"}'
-->
<template>
  <div class='contrast'>
    <toolbar :view-title='viewTitle'>
      <div
        class="leftButton"
        type="button"
        slot="left-button" @click="goBack">
      </div>
      <div
        class="rightButton"
        type="button"
        slot='right-button'>
        <div class="filterButton" @click="showFilter" v-show="!filterOpenState">
          <span class="filterIcon" :page-id="routeKey">&nbsp;</span>
          <span>筛选</span>
        </div>
        <div class="menuButton" @click="showSlideMenu" v-show="!filterOpenState">
          <span class="menuIcon">&nbsp;</span>
          <span>菜单</span>
        </div>
        <span v-show="filterOpenState" @click="doFilter" class="filterOver">完成</span>
      </div>
    </toolbar>
    <div class="overall-comparison">
      <select-for-product :select-a-text="'选择产品A'"
                          :select-b-text="'选择产品B'"
                          :model-a="modelA"
                          :model-b="modelB"
                          :default-org-id="defaultOrgId"
                          :default-category="defaultCategory"
                          :default-category-id="defaultCategoryId"></select-for-product>
      <div class="filter-message-box clearfix">
        <a  v-if="defaultCategory !== ''" class="filter-condition" @click="showFilter('pinlei')" href="javascript:;">{{defaultCategory}}</a>
        <span>
          <a v-if="UrlParams.platform !== ''" class="filter-condition" @click="showFilter('pingtai')" href="javascript:;">{{pingtai}}</a>
          <a v-if="UrlParams.platform === ''" class="filter-condition" @click="showFilter('pingtai')" href="javascript:;">所有平台</a>
        </span>
        <a class="time filter-condition" @click="showFilter('shijian')" href="javascript:;">{{UrlParams.startTime | formatDate}} - {{UrlParams.endTime | formatDate}}</a>
      </div>
      <div class="split-line"></div>
      <div class="clearfix">
        <ul class="thead display-flex">
          <li class="">对比参数</li>
          <li @click="goPath(0)">{{productA.replace('|', '')}}</li>
          <li @click="goPath(1)">{{productB.replace('|', '')}}</li>
        </ul>
      </div>
      <div class="table-view clearfix">
        <div class="tbody">
          <ul class="params">
            <li v-for="item in paramsNames" track-by="$index">{{item}}</li>
          </ul>
          <ul class="brand" v-for="brand in tableRows" track-by="$index">
            <li class="item">
              <img :src="brand[gridColumns[0]] || '../../../static/img/125125.png'">
            </li>
            <li class="item" v-for="item in gridColumns" track-by="$index" v-html="brand[item]" v-show="$index !== 0"></li>
          </ul>
        </div>
      </div>

      <filter-view v-show="filterOpenState"
                   :condition="conditions"
                   :page-id="routeKey"
                   :default-start-date="UrlParams.startTime"
                   :default-end-date="UrlParams.endTime"
                   :choose-platforms= "pingtai"
                   :choose-platforms-id= "pingtaiId"
                   :default-org-id="defaultOrgId"
                   :default-category="defaultCategory"
                   :default-category-id="defaultCategoryId"></filter-view>
      <slide-menu v-show="menuOpenState"></slide-menu>
      <page-turning :mkey="routeKey" :page-id="'peer'"></page-turning>
      <div class="loading-box">
        <img src="../../../../static/img/loading.gif" class="loading">
      </div>
      <confirm :show.sync="timeoutStatus"
       title="加载失败,您可以"
       cancel-text="返回首页"
       confirm-text="重新加载"
       @on-cancel="this.$route.router.go('/home')"
       @on-confirm="reload"></confirm>
    </div>
  </div>
</template>

<script>
  import Toolbar from '../../common/Toolbar.vue'
  import FilterView from '../../common/FilterView.vue'
  import SelectForProduct from '../SelectForProduct.vue'
  import SlideMenu from '../SlideMenu.vue'
  import pageTurning from '../../common/pageTurning.vue'
  import Confirm from 'vux/dist/components/Confirm'

  export default{
    components: {
      Toolbar,
      FilterView,
      SelectForProduct,
      SlideMenu,
      pageTurning,
      Confirm
    },
    data: function () {
      return {
        pingtai: '所有平台',
        pingtaiId: '',
        timeoutStatus: false,
        saleUnit: '',
        routeKey: '',
        menuOpenState: false,
        selectAText: '选择产品A',
        selectBText: '选择产品B',
        viewTitle: '产品整体对标',
        filterOpenState: false,
        tableTitle: '',
        tableRows: [],
        brandA: '',
        brandB: '',
        productA: '',
        productB: '',
        modelA: '',
        modelAID: '',
        modelB: '',
        modelBID: '',
        defaultOrgId: '',
        defaultCategory: '',
        defaultCategoryId: '',
        gridColumns: ['img_url', 'sales', 'sales_amount', 'sale_price', 'all_comment', 'bad_comment'],
        paramsNames: ['图片', '销量', '销额（万元）', '均价（元）', '评价数', '差评数'],
        UrlParams: {
          pageId: 68,
          mkey: 'productOverallComparison',
          dateSeg: 'other',
          startTime: '',
          endTime: '',
          category_id: '',
          category: '',
          brand: '',
          product: '',
          model: '',
          model_id: '',
          org_code: '',
          platform: ''
        }
      }
    },
    methods: {
      goPath: function (index) {
        let path = `/industry/productInfo?category_id=${this.defaultCategoryId}&category=${this.defaultCategory}&org_id=${this.defaultOrgId}&`
        if (index === 0) {
          path += `brand=${this.brandA}&model=${this.modelA}`
        } else {
          path += `brand=${this.brandB}&model=${this.modelB}`
        }
        console.log('this.modelB', path)
        this.$router.go(path)
      },
      reload: function () {
        window.location.reload()
      },
      goBack: function () {
        window.history.back()
      },
//      显示筛选
      showFilter: function () {
        this.filterOpenState = true
      },
//      显示页面导航侧栏
      showSlideMenu: function () {
        this.menuOpenState = !this.menuOpenState
        var self = this
        setTimeout(function () {
          self.$broadcast('slideStart')
        }, 0)
      },
//      执行筛选
      doFilter: function () {
        this.$broadcast('doFilter')
        this.filterOpenState = false
      },
//      获取数据后更新 tableRows内容
      getData: function (params) {
//        this.$dispatch('changeLoadingStatus', true)
        var _this = this
        this.$http.post(window.interface.peerData, params, {_timeout: 30000}).then(function (res) {
          if (res.json().error) {
            this.timeoutStatus = true
            return
          }
          document.getElementsByClassName('loading-box')[0].style.display = 'none'
          this.$dispatch('changeLoadingStatus', false)
          var data = res.json()
          this.pingtai = data.platformCnd || data.platform
          this.pingtaiId = data.platform_id
          _this.defaultCategoryId = data.category_id || ''
          _this.defaultCategoryId = _this.UrlParams.category_id = data.category_id || ''
          _this.defaultCategory = _this.UrlParams.category = data.category || ''
          _this.defaultOrgId = _this.UrlParams.org_code = data.org_code || ''
          _this.modelA = data.model.split(',')[0] || ''
          _this.modelAID = data.model_id ? data.model_id.split(',')[0] : ''
          _this.modelB = data.model.split(',')[1] || ''
          _this.modelBID = data.model_id ? data.model_id.split(',')[1] : ''
          _this.brandA = data.brand.split(',')[0] || ''
          _this.brandB = data.brand.split(',')[1] || ''
          _this.paramsNames[1] = '销量' + data.chartDatas[0].saleUnit
          _this.UrlParams.startTime = data.startTime || ''
          _this.UrlParams.endTime = data.endTime || ''
          _this.viewTitle = data.title
          _this.productA = data.product ? data.product.split(',')[0] : ''
          _this.productB = data.product ? data.product.split(',')[1] : ''
          _this.tableRows = data.chartDatas ? data.chartDatas[0].detail : ''
          var product1Params = data.chartDatas[0].detail[0] ? data.chartDatas[0].detail[0].params : []
          var product2Params = data.chartDatas[0].detail[1] ? data.chartDatas[0].detail[1].params : []
          this.$broadcast('loadDataDone')
          this.paramsNames.splice(6, 99)
          this.gridColumns.splice(6, 99)
          var hasProp = false
          for (let x in product1Params) {
            hasProp = true
            this.paramsNames.push(x)
            this.gridColumns.push(x)
            this.tableRows[0][x] = product1Params[x]
            this.tableRows[1][x] = product2Params[x]
          }
          if (!hasProp) {
          }
          // _this.modelA = data.chartDatas ? data.chartDatas[0].detail[0].model : []
          // _this.modelB = data.chartDatas ? data.chartDatas[0].detail[1].model : []
        }, (res) => {
          _this.timeoutStatus = true
        })
      }
    },
    events: {
//      接收filter组件回传回来的值，然后再根据值重新请求页面数据
      'filterConditions': function (filterConditions) {
        this.UrlParams.department = filterConditions.department || ''
        this.UrlParams.category = filterConditions.category || ''
        this.UrlParams.category_id = this.defaultCategoryId = filterConditions.category_id || ''
        this.UrlParams.platform = filterConditions.platform || ''
        this.UrlParams.platform_id = filterConditions.platform_id || ''
        this.UrlParams.startTime = filterConditions.starttime || ''
        this.UrlParams.endTime = filterConditions.endtime || ''
        this.UrlParams.dateSeg = filterConditions.dateSeg || ''
        this.UrlParams.org_code = filterConditions.org_code || ''
        this.UrlParams.brand = ''
        this.UrlParams.product = ''
        this.UrlParams.model = ''
        this.UrlParams.model_id = ''
        document.getElementsByClassName('loading-box')[0].style.display = 'block'
        this.getData(this.UrlParams)
      },
//      选择产品后重新获取数据
      'chooseProductA': function (row) {
        this.brandA = row.brand
        this.modelA = row.model
        this.modelAID = row.model_id
        this.productA = row.brand + '|' + row.model
        if (this.productB.indexOf('选择产品') === -1) {
          this.UrlParams.brand = this.brandA + ',' + this.brandB
          this.UrlParams.model = this.modelA + ',' + this.modelB
          this.UrlParams.model_id = this.modelAID + ',' + this.modelBID
          this.UrlParams.product = this.productA + ',' + this.productB
          document.getElementsByClassName('loading-box')[0].style.display = 'block'
          this.getData(this.UrlParams)
        }
      },
      'chooseProductB': function (row) {
        this.brandB = row.brand || ''
        this.modelB = row.model || ''
        this.modelBID = row.model_id || ''
        this.productB = row.brand + '|' + row.model
        this.UrlParams.brand = this.brandA + ',' + this.brandB
        this.UrlParams.model = this.modelA + ',' + this.modelB
        this.UrlParams.model_id = this.modelAID + ',' + this.modelBID
        this.UrlParams.product = this.productA + ',' + this.productB
        document.getElementsByClassName('loading-box')[0].style.display = 'block'
        this.getData(this.UrlParams)
      }
    },
    ready: function () {
    },
    created: function () {
      this.routeKey = this.$route.query.key
      var brand1 = this.$route.query.brand1
      var brand2 = this.$route.query.brand2
      var model1 = this.$route.query.model1
      var model2 = this.$route.query.model2
      var model1ID = this.$route.query.model1_id
      var model2ID = this.$route.query.model2_id
      this.UrlParams.model_id = model1ID + ',' + model2ID
      this.defaultCategory = this.$route.query.defaultCategory
      this.UrlParams.brand = this.$route.query.brand1 ? brand1 + ',' + brand2 : ''
      this.UrlParams.model = this.$route.query.brand1 ? model1 + ',' + model2 : ''
      this.UrlParams.product = this.$route.query.brand1 ? brand1 + '|' + model1 + ',' + brand2 + '|' + model2 : ''
      this.UrlParams.category_id = this.$route.query.categoryId || ''
      this.getData(this.UrlParams)
    }
  }
</script>

<style lang='scss' scoped>
  .overall-comparison {
    margin-top: 8vh;
    margin-bottom: 5vh;

    img{
      max-width: 90%;
      max-height: 150%;
    }
  }

  .table-view {
    position: relative;
    height: 52.5vh;
    overflow-y: scroll;
  }

  .tbody {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    max-height: 52vh;
    overflow-y: scroll;
  }

  .thead {
    width: 100%;
    align-items: center;
    background: rgba(188, 219, 255, .4);
  }

  .thead > li {
    flex: 1;
    padding: .4rem 0;
    height: 100%;
    text-align: center;
    text-decoration: underline;
    white-space: initial;
    word-break: break-all;

    &:first-child {
      text-decoration: none;
    }
  }

  ul {
    float: left;
    width: 33.3%;
    text-align: center;

  li {
    height: .8rem;
    text-overflow:ellipsis;
    display:-webkit-box;
    -webkit-box-orient:vertical;
    -webkit-line-clamp:2;
    overflow: hidden;
  }

  }
  .brand{
    background: rgba(244, 249, 255, .4);
  }

  .brand li:nth-child(1), .params li:nth-child(1){
    height: 3rem;
    line-height: 3rem;
  }
  .params {
    box-sizing:border-box;
    border-right: 1px solid  rgba(0, 0, 0, .1);
  }

  .split-line {
    width: 100%;
    height: 5px;
    background: #f2f2f2;
  }

  .filter-message-box{
    padding: 0rem .2rem .2rem;
    font-size: 0.28rem;
  }
</style>
